package com.ebuy.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ebuy.dao.ProductDAO;
import com.ebuy.dao.ProductTypeDAO;
import com.ebuy.model.Customer;
import com.ebuy.model.Product;
import com.ebuy.model.ProductType;

public class ProductServlet extends BaseServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

	private void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getSession().getAttribute("loggedIn") == "true") {
			ProductDAO dao = new ProductDAO();
			ProductTypeDAO dao2 = new ProductTypeDAO();

			ArrayList<ProductType> productType = dao2.getAllProductType();
			request.setAttribute("productType", productType);
			
			ArrayList<Product> products = dao.getAllProduct();
			request.setAttribute("products", products);

			Customer customer = (Customer) request.getSession().getAttribute(
					"customer");
			request.setAttribute("customer", customer);

			forward(request, response, "/product.jsp");
		} else {
			redirect(response, "", false);
		}
	}

}
